package com.jacen.etl.util;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jodd.util.StringUtil;

public class ServletUtils {
	
	
	public static void outputHTML(HttpServletResponse response, String sContent)
	{
		try
		{
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.write(sContent);
			out.flush();
		}
		catch (Exception e)
		{
			throw new RuntimeException(e);
		}
	}
	
	public static Map<String, String> getParameterMap(HttpServletRequest request)
	  {
	    Enumeration<?> paramNames = request.getParameterNames();

	    Map<String, String> paramMap = new HashMap<String, String>();
	    while (paramNames.hasMoreElements()) {
	      String paramName = (String)paramNames.nextElement();
	      paramMap.put(paramName, request.getParameter(paramName));
	    }
	    return paramMap;
	  }

	@SuppressWarnings("unchecked")
	public static <T> T getParamValue(Class<T> clazz, Map<String, String> paramsMap, String paramName)
	  {
	    try
	    {
	      String paramValue = (String)paramsMap.get(paramName);
	      if (StringUtil.isEmpty(paramValue)) throw new Exception("参数值为空");

	      if(clazz.equals(String.class))
	        return (T)paramValue;
	      if(clazz.equals(Timestamp.class)){
	        return (T)DateUtils.str2Timestamp(paramValue, "yyyy-MM-dd HH:mm:ss");
	      }
	      if(clazz.equals(Date.class)){
	    	  return (T)DateUtils.str2Date(paramValue, "yyyy-MM-dd HH:mm:ss");
	      }

	      return (T)clazz.getMethod("valueOf", new Class[] { String.class }).invoke(null, new Object[] { paramValue });
	    }
	    catch (Exception e) {
	    }
	    return null;
	  }

	  @SuppressWarnings("unchecked")
	  public static <T> T getParamValue(HttpServletRequest request, String paramName, Class<T> clazz)
	  {
	    try
	    {
	      String paramValue = HttpParamCaster.getUTF8Parameter(request, paramName);
	      if (StringUtil.isEmpty(paramValue)) throw new Exception();

	      if (clazz.equals(String.class))
	        return (T)paramValue;
	      if ((clazz.equals(Timestamp.class)) || (clazz.equals(Date.class)))
	      {
	        return (T)DateUtils.str2Timestamp(paramValue, "yyyy-MM-dd HH:mm:ss");
	      }

	      return (T)clazz.getMethod("valueOf", new Class[] { String.class }).invoke(null, new Object[] { paramValue });
	    }
	    catch (Exception e) {
	    }
	    return null;
	  }

	  public static <T> T getEntity(HttpServletRequest request, Class<T> clazz)
	  {
	    return getEntity(request, clazz, "yyyy-MM-dd HH:mm:ss");
	  }

	  @SuppressWarnings("unchecked")
	  public static <T> T getEntity(HttpServletRequest request, Class<T> clazz, String dateFormat)
	  {
	    if (clazz == null) throw new RuntimeException("clazz不能为空");
	    if (request == null) throw new RuntimeException("request不能为空");
	    if (dateFormat == null) throw new RuntimeException("dateFormat不能为空");
	    try{
	      Object obj = clazz.newInstance();
	      updateEntity(request, obj, dateFormat);
	      return (T)obj; 
	      } catch (Exception e) {
	    	  throw new RuntimeException(e);
	    }
	    
	  }

	  public static void updateEntity(HttpServletRequest request, Object entity)
	  {
	    updateEntity(request, entity, "yyyy-MM-dd HH:mm:ss");
	  }

	  public static void updateEntity(HttpServletRequest request, Object entity, String dateFormat)
	  {
	    if (entity == null) throw new RuntimeException("entity不能为空");
	    if (request == null) throw new RuntimeException("request不能为空");
	    if (dateFormat == null) throw new RuntimeException("dateFormat不能为空");
	    try
	    {
	      Enumeration<?> em = request.getParameterNames();
	      while (em.hasMoreElements()) {
	        String fieldName = (String)em.nextElement();
	        if (!BeanUtils.hasProperty(entity.getClass(), fieldName))
	          continue;
	        String fieldValue = HttpParamCaster.getUTF8Parameter(request, fieldName);
	        if (StringUtil.isEmpty(fieldValue)) {
	          BeanUtils.setProperty(entity, fieldName, null);
	          continue;
	        }

	        Class<?> fieldType = BeanUtils.getPropertyType(entity.getClass(), fieldName);

	        Object value = null;
	        if (fieldType == String.class)
	          value = fieldValue;
	        else if ((fieldType == Timestamp.class) || (fieldType == Date.class))
	          value = DateUtils.str2Timestamp(fieldValue, dateFormat);
	        else {
	          value = fieldType.getMethod("valueOf", new Class[] { String.class }).invoke(null, new Object[] { fieldValue });
	        }
	        BeanUtils.setProperty(entity, fieldName, value);
	      }
	    } catch (Exception e) {
	      throw new RuntimeException(e);
	    }
	  }
}
